package com.hogofwar.DJbot;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;

import org.jibble.pircbot.IrcException;
import org.jibble.pircbot.NickAlreadyInUseException;

import bsh.EvalError;

public class MainRun {
	public static ArrayList<IRCBase> inst = new ArrayList<IRCBase>();

	/**
	 * @param args
	 * @throws EvalError
	 * @throws IrcException
	 * @throws IOException
	 * @throws NickAlreadyInUseException
	 */
	public static void main(String[] args) throws NickAlreadyInUseException,
			IOException, IrcException, EvalError {
		// TODO Auto-generated method stub
		String[] servers = loadProp("servers").split(",");
		for (String s : servers) {
			System.out.println(s);
			// new IRCBase(s);
			new MyThread(s).start();
		}
		
	}

	public static String loadProp(String prop) {
		File file = new File("config.conf");

		// Does the file already exist
		if (!file.exists()) {
			try {
				// Try creating the file
				file.createNewFile();
			} catch (IOException ioe) {
				ioe.printStackTrace();
			}
		}
		try {
			Properties properties = new Properties();
			properties.load(new FileInputStream("config.conf"));

			return properties.getProperty(prop, "pleasereplace");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

	}
}

class MyThread extends Thread {

	private String to;

	public MyThread(String to) {
		this.to = to;
	}

	@Override
	public void run() {
		System.out.println("hello " + to);
		try {
			IRCBase base = new IRCBase(to);
			MainRun.inst.add(base);
		} catch (NickAlreadyInUseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IrcException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (EvalError e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
